package uit.shoeshop.dao;
import uit.shoeshop.dto.CategoryDTO;
import uit.shoeshop.dto.CityDTO;
import uit.shoeshop.lib.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CityDAO {
	private Connection conn;
	private String tableName;
	
	public CityDAO(){
		setConn(null);
		setTableName(Constants.TBL_CITY);
	}

	public ArrayList<CityDTO> getAllCity() throws SQLException{
		ArrayList<CityDTO> results = new ArrayList<CityDTO>();
		
		try{
			setConn(Util.Connect());
			String sql = "SELECT * FROM " + getTableName();
			PreparedStatement stm = getConn().prepareStatement(sql);
			
			//thuc hien cau lenh sql
			ResultSet rs = stm.executeQuery();
			
			while (rs.next()) {
				CityDTO city = new CityDTO();
				city.setCity_id(rs.getInt("city_id"));
				city.setName(rs.getString("name"));
				results.add(city);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(getConn() != null)
				getConn().close();
		}
		return results;
	}	
	
	/**
	 * @param conn the conn to set
	 */
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	/**
	 * @return the conn
	 */
	public Connection getConn() {
		return conn;
	}

	/**
	 * @param tableName the tableName to set
	 */
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	/**
	 * @return the tableName
	 */
	public String getTableName() {
		return tableName;
	}

}
